package com.topisv.tms.service;

import java.util.Date;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.ReturnExceptionInfo;
import com.topisv.tms.entity.work.WorkOrder;
import com.topisv.tms.repository.ReturnExceptionInfoDao;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.service.base.BaseService;
import com.topisv.tms.service.work.WorkOrderService;

//Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly = true)
public class ReturnExceptionInfoService extends BaseService{

	@Autowired
	private ReturnExceptionInfoDao returnExceptionInfoDao;
	@Autowired
	private WorkOrderService workOrderService;
	
	@Transactional(readOnly = false)
	public void save(ReturnExceptionInfo rei){
		returnExceptionInfoDao.save(rei);
		return;
	}
	
	@Transactional(readOnly = false)
	public void finish(ReturnExceptionInfo rei){
		WorkOrder workOrder = workOrderService.loadByAnyOrderno(rei.getOrderNo());
		ShiroUser su = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		workOrder.setSendOutStatus("finish");
		
		rei.setFinishBy(su.getJobuuid());
		rei.setFinishTime(new Date());
		
		workOrderService.save(workOrder);
		returnExceptionInfoDao.save(rei);
		return;
	}
	
	@Transactional(readOnly = true)
	public ReturnExceptionInfo findOne(long id){
		return returnExceptionInfoDao.findOne(id);
	}
	
	@Transactional(readOnly = true)
	public ReturnExceptionInfo findByTaskCodeAndOrderNo(String taskCode,String orderNo){
		return returnExceptionInfoDao.findByTaskCodeAndOrderNo(taskCode,orderNo);
	}

}
